package com.projet.whattowatch.activity;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.adapter.MovieAdapter;
import com.projet.whattowatch.objet.Film;
import com.projet.whattowtach.utils.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class SmartRequestFragment extends Fragment
{
	ListView mSmartRequest;
	Button top1;
	Button top2;
	Button top3;
	
	String image_url;
	String image_url2;
	String image_url3;
	Button moreButton;
	
	final String EXTRA_MOVIE_NAME = "movie_name";
	final String EXTRA_MOVIE = "movie_name";
	final String EXTRA_MOVIE_URL = "movie_url";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View rootView = inflater.inflate(R.layout.smart_request_layout, container, false);
		mSmartRequest = (ListView) rootView.findViewById(R.id.smartRequestListView);
		mSmartRequest.setOnItemClickListener(new OnItemClickListener()
		{
		    
		    @Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		    { 
		    	String movieName = mSmartRequest.getItemAtPosition(position).toString();
		    	
		    	Intent movieDetail = new Intent(getActivity().getApplicationContext(), MovieDetailActivity.class);
		    	movieDetail.putExtra(EXTRA_MOVIE_NAME, movieName);
		    	startActivity(movieDetail);
		    }
		 });
		
		ArrayList<Film> listFilm = ConnectedActivity.mUtilisateur.smartRequest(MainActivity.mLibFilm);
		ArrayList<String> imageMovie = new ArrayList<String>();
		ArrayList<String> listNomFilm = new ArrayList<String>();
		for (int i = listFilm.size()-4; i > listFilm.size()-16; i--)
		{
			listNomFilm.add(listFilm.get(i).getNom());
			imageMovie.add(listFilm.get(i).getImageUrl());
		}
		
		top1 = (Button) rootView.findViewById(R.id.top1);
		top2 = (Button) rootView.findViewById(R.id.top2);
		top3 = (Button) rootView.findViewById(R.id.top3);
		
		Animation animTop1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.anim_top1);
		animTop1.setInterpolator(getActivity().getApplicationContext(), android.R.anim.bounce_interpolator);
		top1.startAnimation(animTop1);
		
		Animation animTop2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.anim_top2);
		animTop2.setInterpolator(getActivity().getApplicationContext(), android.R.anim.bounce_interpolator);
		top2.startAnimation(animTop2);
		
		Animation animTop3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.anim_top3);
		animTop3.setInterpolator(getActivity().getApplicationContext(), android.R.anim.bounce_interpolator);
		top3.startAnimation(animTop3);
		
		final Vibrator v = (Vibrator) getActivity().getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);

		Runnable task = new Runnable()
		{ 
		    @Override
			public void run()
		    {
		    	v.vibrate(200);
		    }
		};
		
		Handler handler = new Handler();
		handler.postDelayed(task, 1010);
		handler.postDelayed(task, 1710);
		
		Runnable taskLong = new Runnable()
		{ 
		    @Override
			public void run()
		    {
		    	v.vibrate(500);
		    }
		};
		
		handler.postDelayed(taskLong, 2410);
		
		image_url = listFilm.get(listFilm.size()-1).getImageUrl();
		image_url2 = listFilm.get(listFilm.size()-2).getImageUrl();
		image_url3 = listFilm.get(listFilm.size()-3).getImageUrl();
	 
		int loader = R.drawable.ic_action_settings;
		ImageLoader imgLoader = new ImageLoader(getActivity().getApplicationContext());
		imgLoader.DisplayImage(image_url, loader, top1);
		imgLoader.DisplayImage(image_url2, loader, top2);
		imgLoader.DisplayImage(image_url3, loader, top3);
		
		moreButton = (Button) rootView.findViewById(R.id.moreButton);
		moreButton.setOnClickListener(moreButtonListener);
		 
		MovieAdapter adapter = new MovieAdapter(getActivity(), listNomFilm, imageMovie);
		mSmartRequest.setAdapter(adapter);
				
		top1.setOnClickListener(top1Listener);
		top2.setOnClickListener(top2Listener);
		top3.setOnClickListener(top3Listener);
			
		return rootView;
	}
	
	public OnClickListener top1Listener = new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
        	String nomFilm = null;
        	for (int i = 0; i < MainActivity.mLibFilm.getListFilm().size(); i++) 
        	{
        		if (MainActivity.mLibFilm.getFilm(i).getImageUrl().equals(image_url))
        		{
        			nomFilm = MainActivity.mLibFilm.getFilm(i).getNom();
        		}
			}
        	
        	Intent smartMovie = new Intent(getActivity().getApplicationContext(), MovieDetailActivity.class);
        	smartMovie.putExtra(EXTRA_MOVIE, nomFilm);
	    	startActivity(smartMovie);
        }
    };
	
    public OnClickListener top2Listener = new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
        	String nomFilm = null;
        	for (int i = 0; i < MainActivity.mLibFilm.getListFilm().size(); i++) 
        	{
        		if (MainActivity.mLibFilm.getFilm(i).getImageUrl().equals(image_url2))
        		{
        			nomFilm = MainActivity.mLibFilm.getFilm(i).getNom();
        		}
			}
        	
        	Intent smartMovie = new Intent(getActivity().getApplicationContext(), MovieDetailActivity.class);
        	smartMovie.putExtra(EXTRA_MOVIE, nomFilm);
	    	startActivity(smartMovie);
        }
    };
    public OnClickListener top3Listener = new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
        	String nomFilm = null;
        	for (int i = 0; i < MainActivity.mLibFilm.getListFilm().size(); i++) 
        	{
        		if (MainActivity.mLibFilm.getFilm(i).getImageUrl().equals(image_url3))
        		{
        			nomFilm = MainActivity.mLibFilm.getFilm(i).getNom();
        		}
			}
        	
        	Intent smartMovie = new Intent(getActivity().getApplicationContext(), MovieDetailActivity.class);
        	smartMovie.putExtra(EXTRA_MOVIE, nomFilm);
	    	startActivity(smartMovie);
        }
    };
    
    public OnClickListener moreButtonListener = new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
        	mSmartRequest.setVisibility(1);
        	moreButton.setVisibility(View.GONE);
        }
    };
}